package com.example.springback.service;

import com.example.springback.domain.Employee;
import com.example.springback.domain.Enterprise;
import com.example.springback.domain.Pension;
import com.example.springback.exception.CustomException;
import com.example.springback.mapper.EmployeeMapper;
import com.example.springback.mapper.EnterpriseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.example.springback.mapper.PensionMapper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
public class PensionService {
    @Resource
    private PensionMapper pensionMapper;
    @Resource
    private EnterpriseMapper enterpriseMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    public PageInfo<Enterprise> selectPage(String enterpriseName, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<Enterprise> pensions=pensionMapper.selectAll(enterpriseName);
        return PageInfo.of(pensions);
    }

    public void add(Pension pension,Enterprise enterprise) {
        Enterprise enterprise1=enterpriseMapper.selectByPrimaryKey(pension.getEnterpriseId());
        //**************验证企业信息************
        if(enterprise==null){
            throw new CustomException("500","企业不存在，请检查输入的纳税人识别号");
        }
        if(!Objects.equals(enterprise.getTaxNumber(), enterprise1.getTaxNumber()) || !Objects.equals(enterprise.getEnterpriseName(), enterprise1.getEnterpriseName())
                || !Objects.equals(enterprise.getBossName(), enterprise1.getBossName()) || !Objects.equals(enterprise.getPhone(), enterprise1.getPhone())){
            throw new CustomException("500","输入的信息有误");
        }
        //一个月只能缴纳一次
        Pension pension1= pensionMapper.selectByEnterpriseAndTerm(pension);
        if(pension1!=null){
            throw new CustomException("500","当前属期已缴纳，不可重复缴纳");
        }
        //*******************设置缴纳日期*************
        Date date = new Date(); // 获取当前日期
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); // 定义日期格式
        String formattedDate = formatter.format(date); // 格式化日期
        pension.setPensionTime(formattedDate);
        //***************设置征集流水号**************
        Integer payNumber=pensionMapper.selectTodayPayNum(formattedDate);//获取当天缴纳数量
        SimpleDateFormat formatter1 = new SimpleDateFormat("yyyyMMdd"); // 定义日期格式
        String formattedDate1 = formatter1.format(date); // 格式化日期
        String formattedNumber = String.format("%06d", payNumber+1); // 格式化为6位，不足补0
        String serialNumber="TSYL"+formattedDate1+formattedNumber;//征集流水号
        pension.setSerialNumber(serialNumber);
        //**************设置缴纳金额***************
        Employee employee=new Employee();
        Double pensionMoney=0.0;
        employee.setEnterpriseId(enterprise1.getEnterpriseId());
        employee.setIsleave(0);//给在职员工缴纳
        List<Employee> employeeList=employeeMapper.selectAll(employee);//通过企业id查询企业员工
        if(employeeList.size()==0){
            throw new CustomException("500","请先导入员工信息");
        }
        for (Employee e:employeeList){
            pensionMoney=pensionMoney+e.getEmployeeSalary()*0.12;//将公司为每个员工要缴纳的金额相加
            e.setEmployeePension(e.getEmployeeSalary()*0.08);//放入员工个人账户中的
            employeeMapper.updateByPrimaryKey(e);
        }
        pension.setPensionMoney(pensionMoney);
        pensionMapper.insert(pension);
    }

    public void update(Pension pension) {
        pensionMapper.updateByPrimaryKey(pension);

    }


    public void updateBatch(List<Pension> pensionList) {

    }

    public PageInfo<Pension> selectEnterprisePage(Pension pension, Integer pageNumber, Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<Pension> pensions=pensionMapper.selectEnterprise(pension);
        return PageInfo.of(pensions);
    }

    public List<Map<String, Object>> getMonthlyTrend(int months) {
        List<Map<String, Object>> maps=pensionMapper.getMonthlyTrend(months);
        return maps;
    }

    public List<Map<String, Object>> getMonthlyPensionByEnterprise(String month) {
        return pensionMapper.selectMonthlyPensionByEnterprise(month);
    }

    public double sumAllPension(String month) {
       return pensionMapper.sumAllPension(month);
    }
}
